﻿// Exercise 3:
// Write a method that from a given array of students finds all students
// whose first name is before its last name alphabetically. 
// Use LINQ query operators.

// Exercise 4:
// Write a LINQ query that finds the first name and last name of all students with age between 18 and 24.

// Exercise 5:
// Using the extension methods OrderBy() and ThenBy() with lambda expressions 
// sort the students by first name and last name in descending order. Rewrite the same with LINQ.

namespace _03.StudentsFirstNameIsBeforeItsLast
{
    using System;
    using System.Collections;
    using System.Linq;

    public class StudentsFirstNameIsBeforeItsLast
    {
        public static void Main()
        {
            Student[] studentArray = {
                                     new Student("Poncho", "Kurtev", 9),
                                     new Student("Gruyo", "Puichev", 18),
                                     new Student("Spiridon", "Spiridonov", 20),
                                     new Student("Bai", "Ganyo", 40),
                                     new Student("Yanko", "Munchev", 66)
                                     };

            // Exercise 3
            Console.WriteLine("All student with first name before last name alphabetically: ");
            PrintEnumeration(GetStudentWithFirstNameBeforeLast(studentArray));
            Console.WriteLine();

            // Exercise 4
            Console.WriteLine("Get student with age in range between 18 and 24: ");
            PrintEnumeration(GetStudentWithAgeInRange(studentArray));
            Console.WriteLine();

            // Exercise 5 - variant with LINQ 
            Console.WriteLine("Sorted students by First and Last name (using LINQ): ");
            PrintEnumeration(SortDescendingByNamesLINQ(studentArray));
            Console.WriteLine();

            // Exercise 5 - variant with Lambda Expression
            Console.WriteLine("Sorted students by First and Last name (using Lambda expression): ");
            PrintEnumeration(SortDescendingByNamesLambda(studentArray));
            Console.WriteLine();

        }

        public static void PrintEnumeration(IEnumerable sortedStudentArray)
        {
            foreach (var item in sortedStudentArray)
            {
                Console.WriteLine(item);
            }
        }

        // Exercise 3
        public static IEnumerable GetStudentWithFirstNameBeforeLast(Student[] studentArray)
        {
            var sortedStudent = from student in studentArray
                                where student.FirstName.CompareTo(student.LastName) < 0
                                select student;

            return sortedStudent;
        }

        // Exercise 4
        public static IEnumerable GetStudentWithAgeInRange(Student[] studentArray)
        {
            var sortedStudent = from student in studentArray
                                where 18 < student.Age && student.Age < 24
                                select student;

            return sortedStudent;
        }

        // Exercise 5 - variant with LINQ
        public static IEnumerable SortDescendingByNamesLINQ(Student[] studentArray)
        {
            var sortedStudent = from student in studentArray
                                orderby student.FirstName descending,
                                student.LastName descending
                                select student;

            return sortedStudent;
        }

        // Exercise 5 - variant with Lambda Expression
        public static IEnumerable SortDescendingByNamesLambda(Student[] studentArray)
        {
            var sortedStudent = studentArray.OrderByDescending(student => student.FirstName).ThenByDescending(student => student.LastName);

            return sortedStudent;
        }

    }
}